<template>
  <div></div>
</template>

<script>
import { mapGetters } from 'vuex';
import Utils from '@/common/pakjUtils';
import { Dialog } from 'mand-mobile';
export default {
  name: 'Splash',
  data() {
    return {
      route: undefined,
      uid: undefined,
      oid: undefined,
      phone: undefined,
      token: undefined,
      env: undefined
    };
  },
  created() {
    const query = this.$route.query;
    this.route = query.route;
    this.uid = query.uid;
    this.oid = query.oid;
    this.phone = query.phone;
    this.token = query.tk;
    this.env = Utils.getCurrentEnv();
    console.log('currentEvn is ', this.env);
    if (this.token) {
      this.$store.commit('setToken', this.token);
    }
    if (this.route) {
      switch (this.route) {
        case 'Login':
          if (this.env === 'wx' || this.env === 'mini') {
            this.$store.dispatch('wxLogin').then(() => {
              this.goto('Home');
            });
          } else {
            this.goto('Login');
          }
          break;
        case 'Reg':
          if (this.env === 'mini') {
            let params = {
              userPhone: this.phone
            };
            if (this.oid) {
              params.openId = this.oid;
            }
            if (this.uid) {
              params.unionid = this.uid;
            }
            this.$store
              .dispatch('wxReg', params)
              .then(() => {
                this.goto('Home');
              })
              .catch(reason => {
                Dialog.alert({
                  title: '错误',
                  content: reason.message
                });
              });
          } else if (this.env === 'wx') {
            this.goto('Login', {
              action: 'reg',
              mode: 'captcha'
            });
          } else {
            this.goto('Login');
          }
          break;
        case 'CaptchaLogin': {
          this.goto('LoginCaptcha', { phone: this.phone });
          break;
        }
        case 'PasswordLogin': {
          this.goto('LoginPassword', this.$route.query);
          break;
        }
        case 'OrderPayment': {
          this.goto(this.route, { orderno: query.orderno });
          break;
        }
        case 'OrderDetail': {
          this.goto(this.route, { orderno: query.orderno });
          break;
        }
        case 'UserAgreement': {
          this.goto(this.route);
          break;
        }
        default: {
          if (this.env === 'wx' || this.env === 'mini') {
            this.$store.dispatch('wxLogin').then(() => {
              this.goto(this.route);
            });
          } else {
            this.goto(this.route);
          }
          break;
        }
      }
    } else {
      if (query.state && query.code) {
        if (query.state === 'wxauth1') {
          // 通过CODE换取用户登陆信息，如果是新的unionid则进入注册流程
          this.$store
            .dispatch('wxAuth', query.code)
            .then(res => {
              if (res.code === '000000') {
                this.$store.commit('setOpenId', res.data.openId);
                this.$store.commit('setUnionId', res.data.unionId);
                this.$store.commit('setToken', res.data.token);
                this.$store.dispatch('wxLogin').then(() => {
                  let str = localStorage.getItem('WxAuthRouteInfo');
                  if (str && str != '') {
                    let info = JSON.parse(str);
                    if (info.continue) {
                      this.goto(info.routeName, info.query);
                      return;
                    }
                  }
                  this.goto('Home');
                });
              } else if (res.code === '000001') {
                this.$store.commit('setOpenId', res.data.openId);
                this.$store.commit('setUnionId', res.data.unionId);
                this.goto('Login', {
                  action: 'reg',
                  mode: 'captcha'
                });
              }
            })
            .catch(reason => {
              Dialog.alert({
                title: '错误',
                content: reason.message
              });
            });
        } else if (query.state === 'PhoneOrderWxPayment') {
          // 通过CODE换取openid以及unionid，然后进入电话约单支付流程
          this.$store.dispatch('phoneOrderWxAuth', query.code).then(res => {
            console.log(res);
            this.$store.commit('setOpenId', res.openId);
            this.$store.commit('setUnionId', res.unionId);
            let phoneOrderPayInfo = localStorage.getItem('PhoneOrderPayInfo');
            if (phoneOrderPayInfo) {
              phoneOrderPayInfo = JSON.parse(phoneOrderPayInfo);
              localStorage.removeItem('PhoneOrderPayInfo');
              this.goto('PhoneOrderPayment', phoneOrderPayInfo);
            }
          });
        }
      }
    }
  },
  methods: {
    goto(route, query = {}) {
      this.$router.replace({
        name: route,
        query: query
      });
    }
  },
  computed: {
    ...mapGetters(['currentUser'])
  }
};
</script>
